<?php
/** Report
	* 
	* @package Pharmacy Intervention 
	* @subpackage Controller
	* @category Reports 
	* @author Ray Nowell 
	*/  
class Report extends MY_Controller {

	function Report()
	{
		parent::MY_Controller();	
	}
	
	function _get_doc_ready_functions()
	{
			$ret_val	=	'$(".grid tr").bind("mouseenter mouseleave", function() {
							$(this).toggleClass("over");
							});';		
			return $ret_val;
	}
			 
	function _get_report_tables($filter)
	{
		$intervention_counts	=	$this->Intervention_model->get_intervention_counts($filter); 
		
		$intervention_savings	=	$this->Intervention_model->get_intervention_savings($filter);
		
		$table_for_counts	=	'<table cellspacing="0" cellpadding="4" border="0" class="grid">';
		$table_for_savings	=	'<table cellspacing="0" cellpadding="4" border="0" class="grid">';
		$table_for_counts	.=	'<tbody>';
		$table_for_savings	.=	'<tbody>';
		$table_for_counts	.=	'<caption>Intervention Count Stats</caption>';
		$table_for_savings	.=	'<caption>Intervention Savings Stats</caption>';
		$table_for_counts	.=	'<tr class="gridHeader"><th>'.'Code'.'</th><th>'.'Accepted'.'</th><th>'.'Pending'.'</th><th>'.'Rejected'.'</th><th>'.'RM'.'</th><th>'.'Totals'.'</th><th>'.'Pct'.'</th></tr>';
		$table_for_savings	.=	'<tr class="gridHeader"><th>'.'Code'.'</th><th>'.'Accepted'.'</th><th>'.'Pending'.'</th><th>'.'Rejected'.'</th></tr>';
		
		$total_intervention_count = 0;
		$total_accepted_count = 0;
		$total_pending_count = 0;
		$total_rejected_count = 0;
		$total_risk_mitigation_count = 0;		
			
		$total_column_percent = 0.00;
		
		foreach ($intervention_counts as $row)
			$total_intervention_count	+=	$row['Accepted'] + $row['Pending'] + $row['Rejected'];
			
		if ($total_intervention_count == 0)
		{
			$ret_val	=	array(
					'table_for_counts'			=>	'<h1>No Clinical Intervention records match critera.</h1>',
					'table_for_savings'			=>	'',
					'google_chart_count_url'	=>	'',
					'google_chart_savings_url'	=>	'',
				);
			return $ret_val;
		}
		
		$alt_row = false;
		$max_count_count	=	0;
		$report_count_array;
		$legend_code_array;
		foreach ($intervention_counts as $row)
		{
			//Store vars for chart:
			//If at least one of these codes is greater than zero, then we want it in the chart
			if ($row['Accepted'] > 0 || $row['Pending'] > 0 || $row['Rejected'] > 0)
			{
				$legend_count_array[$row['code']]	=	$row['code'];
				$report_count_array[$row['code']]	=	$row['Accepted'].','.$row['Pending'].','.$row['Rejected'];
			}
			
			
			//Set vars for the grid
			$total_accepted_count	+=	$row['Accepted'];
			$total_pending_count	+=	$row['Pending'];
			$total_rejected_count	+=	$row['Rejected'];
			$row_count	=	$row['Accepted'] + $row['Pending'] + $row['Rejected'];
			$total_risk_mitigation_count	=	$row['Rejected'];			
			
			$row_class = 'class = "'.($alt_row ? 'gridAlternatingRow' : 'gridRow').'"';
				
			$table_for_counts	.= '<tr '.$row_class.'><td>'.$row['code'].'</td><td>'.$row['Accepted'].'</td><td>'.$row['Pending'].'</td><td>'.$row['Rejected'].'</td><td>'.$row['Risk_Mitigation'].'</td><td>'.$row['Totals'].'</td><td>'.format_percent($row_count/$total_intervention_count).'</td></tr>';
			//format_percent($row_sum_count/$total_intervention_count);
			$alt_row	=	!$alt_row;
		}
		
		$max_count_count	=	max(array($total_accepted_count,$total_pending_count,$total_rejected_count));
		$count_step			=	$max_count_count > 10 ? round($max_count_count / 10) : 1;
		$google_chart_count_url	=	'http://chart.apis.google.com/chart?';
		$google_chart_count_url	.=	 'chxr=0,0,'.$max_count_count.'|'; //x-axis
		$google_chart_count_url	.=	 '1,0,'.$max_count_count.',1&'; //y-axis
		$google_chart_count_url	.=	'chxt=x,y&';
		$google_chart_count_url	.=	'chxl=0:|A|P|R&';
		$google_chart_count_url	.=	'chbh=a&chs=300x350&cht=bvs&';
		$google_chart_count_url	.=	'chco=ED1C24,FFA3B1,990030,A8E61D,000000,787878,6F3198,2F3699,22B14C&'; //colors
		$google_chart_count_url	.=	'chds=0,'.$max_count_count.',0,'.$max_count_count.'&';
		$google_chart_count_url	.=	'chd=t:'; //get ready for data sets
		$google_chart_count_url	.=	implode('|',$report_count_array).'&';
		$google_chart_count_url	.=	'chdl=';//Get ready for legend
		$google_chart_count_url	.=	str_replace('','',str_replace(' ','%20',implode('|',$legend_count_array).'&'));
		$google_chart_count_url	.=	'chtt=Intervention+Counts';
		
		//echo 'max oount:  '.$max_count_count.'<br>';
		//echo var_dump($report_count_array);
		
		$total_column_percent	=	format_percent(($total_accepted_count + $total_pending_count + $total_rejected_count)/$total_intervention_count);
		
		$total_accepted_savings = 0.00;
		$total_pending_savings = 0.00;
		$total_rejected_savings = 0.00;
		
		$alt_row = false;
		foreach ($intervention_savings as $row)
		{
			
			//Store vars for chart:
			//If at least one of these codes is greater than zero, then we want it in the chart
			if ($row['Accepted'] > 0 || $row['Pending'] > 0 || $row['Rejected'] > 0)
			{
				$legend_savings_array[$row['code']]	=	$row['code'];
				$report_savings_array[$row['code']]	=	$row['Accepted'].','.$row['Pending'].','.$row['Rejected'];
			}
			
			
			$total_accepted_savings		+=	$row['Accepted'];
			$total_pending_savings	 	+=	$row['Pending'];
			$total_rejected_savings 	+=	$row['Rejected'];
						
			$row_class = 'class = "'.($alt_row ? 'gridAlternatingRow' : 'gridRow').'"';
			$table_for_savings	.= '<tr '.$row_class.'><td>'.$row['code'].'</td><td>'.format_currency($row['Accepted']).'</td><td>'.format_currency($row['Pending']).'</td><td>'.format_currency($row['Rejected']).'</td></tr>';
			$alt_row	=	!$alt_row;
		}
		
		$alt_row_count = false;
		$alt_row_savings = false;
		
		$intervention_savings_count	=	max(array($total_accepted_savings,$total_pending_savings,$total_rejected_savings));
		if ($intervention_savings_count	==	0)
			$google_chart_savings_url	=	'';
		else
		{
			$max_savings_count		 	 =	max(array($total_accepted_savings,$total_pending_savings,$total_rejected_savings));
			$savings_count_step			 =	$max_savings_count > 100 ? round($max_savings_count / 10) : 10;
			$google_chart_savings_url	 =	'http://chart.apis.google.com/chart?';
			$google_chart_savings_url	.=	 'chxr=0,0,'.$max_savings_count.'|'; //x-axis
			$google_chart_savings_url	.=	 '1,0,'.$max_savings_count.','.$savings_count_step.'&'; //y-axis
			$google_chart_savings_url	.=	'chxt=x,y&';
			$google_chart_savings_url	.=	'chxl=0:|A|P|R&';
			$google_chart_savings_url	.=	'chbh=a&chs=300x350&cht=bvs&';
			$google_chart_savings_url	.=	'chco=ED1C24,FFA3B1,990030,A8E61D,000000,787878,6F3198,2F3699,22B14C&'; //colors
			$google_chart_savings_url	.=	'chds=0,'.$max_savings_count.',0,'.$max_savings_count.'&';
			$google_chart_savings_url	.=	'chd=t:'; //get ready for data sets
			$google_chart_savings_url	.=	implode('|',$report_savings_array).'&';
			$google_chart_savings_url	.=	'chdl=';//Get ready for legend
			$google_chart_savings_url	.=	str_replace('','',str_replace(' ','%20',implode('|',$legend_savings_array).'&'));
			$google_chart_savings_url	.=	'chtt=Intervention+Savings';
		}
							
		$total_accepted_savings	=	format_currency($total_accepted_savings);
		$total_pending_savings	=	format_currency($total_pending_savings);
		$total_rejected_savings	=	format_currency($total_rejected_savings);
					
		$sum_count_by_column	=	($total_accepted_count + $total_pending_count + $total_rejected_count);
		$total_column_percent	=	($total_column_percent > 100 ? 100 : $total_column_percent);
		$table_for_counts	.=	'<tr class="gridSumRow"><td>'.'Totals'.'</td><td>'.$total_accepted_count.'</td><td>'.$total_pending_count.'</td><td>'.$total_rejected_count.'</td><td>'.$total_risk_mitigation_count.'</td><td>'.$sum_count_by_column.'</td><td>'.$total_column_percent.'</td></tr>';
		$table_for_counts	.=	'<tr class="gridSumRow"><td>'.'Percentages'.'</td><td>'.format_percent($total_accepted_count/$sum_count_by_column).'</td><td>'.format_percent($total_pending_count/$sum_count_by_column).'</td><td>'.format_percent($total_rejected_count/$sum_count_by_column).'</td><td>'.format_percent($total_risk_mitigation_count/$sum_count_by_column).'</td><td>'.format_percent($total_intervention_count/$sum_count_by_column).'</td><td>'.''.'</td></tr>';
		$table_for_savings	.=	'<tr class="gridSumRow"><td>'.'Totals'.'</td><td>'.$total_accepted_savings.'</td><td>'.$total_pending_savings.'</td><td>'.$total_rejected_savings.'</td></tr>';
		$table_for_counts	.=	'</tbody>';
		$table_for_savings	.=	'</tbody>';
		$table_for_counts	.=	'</table>';
		$table_for_savings	.=	'</table>';
		
		$ret_val	=	array(
							'table_for_counts'			=>	$table_for_counts,
							'table_for_savings'			=>	$table_for_savings,
							'google_chart_count_url'	=>	$google_chart_count_url,
							'google_chart_savings_url'	=>	$google_chart_savings_url,
						);
						
		return $ret_val;
		
	}
	
	function index()
	{	  
		$data['additional_javascript_doc_ready_functions'] = $this->_get_doc_ready_functions();
	  
		$data['title'] = COMPANY_NAME." -- Main";
		$data['heading'] = "Report";
		
		//$this->output->enable_profiler(TRUE);
		$this->load->model('Intervention_model');
		$this->load->model('Physician_model');
		
		//Get look-ups for filter form
		$duration_lookup					=	$this->Intervention_model->get_report_date_lookup();
		$data['duration_list']				=	set_lookup($duration_lookup,'filter_field','date_group',BLANK_ROW);
		$physician_lookup					=	$this->Intervention_model->get_physician_lookup();
		$data['physician_list']				=	set_lookup($physician_lookup,'physician_id','full_name',BLANK_ROW);
		$physician_group_lookup				=	$this->Physician_model->get_physician_group_lookup(false);
		$data['physician_group_list']		=	set_lookup($physician_group_lookup,'physician_group_id','group',BLANK_ROW);
		
		$unit_patient_location_lookup		=	$this->Intervention_model->get_unit_patient_location_lookup();
		$data['unit_patient_location_list']	=	set_lookup($unit_patient_location_lookup,'unit_patient_location_id','unit_patient_location',BLANK_ROW);
		$pharmacist_lookup					=	$this->Intervention_model->get_pharmacist_lookup();
		$data['pharmacist_list']			=	set_lookup($pharmacist_lookup,'pharmacist_id','full_name',BLANK_ROW);
		
		$tables	=	$this->_get_report_tables('');
				
		$data['table_for_counts']			=	$tables['table_for_counts'];
		$data['table_for_savings']			=	$tables['table_for_savings'];
		$data['google_chart_count_url']		=	$tables['google_chart_count_url'];	
		$data['google_chart_savings_url']	=	$tables['google_chart_savings_url'];
		
		$data['view'] = 'report_view';
		
		$data['opt_physician'] = array(	'name'		=>	'p_select',
										'id'		=>	'_optPhysician',
										'value'		=>	'Physician',
										'selected'	=>	TRUE,
										);
										
		$data['opt_physician_group']	=	array(	'name'		=>	'p_select',
												'id'		=>	'_optPhysicianGroup',
												'value'		=>	'Physician Group',
												'selected'	=>	TRUE,
											);

		$this->load->vars($data);
		$this->load->view('master_view', $data);	
		

	}
	
	function intervention_report_update()
	{
		//$this->output->enable_profiler(TRUE);
		$this->load->model('Intervention_model');
		
		if ($this->uri->total_segments()	== 3) //If only three segments, then there is not filter
			$filter	=	''; //All filter criteria cleared
		else
			for ( $counter = 4; $counter <= $this->uri->total_segments(); $counter += 2) 
				$filter[$this->uri->segment($counter)]	=	$this->uri->segment($counter + 1);
							
		$r	=	$this->_get_report_tables($filter);
		
		//The google chart url has the pipe | has a delimieter in it.  I delimit that by default in my code.  replace pip with '_DELIM_' to decode during this ajax call
		echo $r['table_for_counts'].'|'.$r['table_for_savings'].'|'.str_replace('|','_DELIM_',$r['google_chart_count_url']).'|'.str_replace('|','_DELIM_',$r['google_chart_savings_url']);
	}
	
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/report.php */
		
		
